package com.sunburst.demo.actor.showcases.user;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Terminated;
import scala.concurrent.Future;

import java.time.Duration;

/**
 * 用户应用
 */
public class UserApp {

    public static void main(String[] args) {
        // 1对1对话示例
        chatDemo1V1();
    }

    /**
     * 1对1对话示例
     */
    public static void chatDemo1V1() {
        // 创建actor system
        ActorSystem actorSystem = ActorSystem.create("userApp");

        ActorRef iKun001ActorRef = actorSystem.actorOf(UserActor.create("A001", 100L), "iKunA001");
        ActorRef iKun666ActorRef = actorSystem.actorOf(UserActor.create("B666", 666L), "iKunB666");

        iKun666ActorRef.tell("Are you iKun? I am " + iKun001ActorRef.toString(), iKun001ActorRef);


        // 等待
        try {
            Thread.sleep(Duration.ofSeconds(120L).toMillis());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        // 终止
        Future<Terminated> terminatedFuture = actorSystem.terminate();

        actorSystem.log().info("==== terminatedFuture.isCompleted: {} ", terminatedFuture.isCompleted());
    }


}
